<?php


namespace app\system\service;

class FeedbackService
{
	public static function get_data_list($options)
	{
		$where[] = ["appid", "=", $options["appid"]];
		if (isset($options["id"]) && $options["id"] != "") {
			$where[] = ["id", "=", $options["id"]];
		}
		if (isset($options["classify_id"]) && $options["classify_id"] != "") {
			$where[] = ["classify_id", "=", $options["classify_id"]];
		}
		if (isset($options["read_status"]) && $options["read_status"] != "") {
			$where[] = ["read_status", "=", $options["read_status"]];
		}
		if (isset($options["handle_status"]) && $options["handle_status"] != "") {
			$where[] = ["handle_status", "=", $options["handle_status"]];
		}
		if (isset($options["keyword"]) && $options["keyword"] != "") {
			$where[] = ["", "exp", \think\Db::raw("title like '%{$options["keyword"]}%' or content like '%{$options["keyword"]}%'")];
		}
		$list = \think\Db::name("app_feedback_list")->where($where)->order(["id" => "desc"])->page($options["page"], $options["limit"])->select();
		$count = \think\Db::name("app_feedback_list")->where($where)->count();
		$pages = ceil($count / $options["limit"]);
		$data = [];
		foreach ($list as $item) {
			$data[] = self::get_array_info($item);
		}
		return ["code" => \app\system\model\Code::success, "data" => $data, "count" => $count, "pages" => $pages];
	}
	public static function get_array_info($item)
	{
		$data = $item;
		$data["classify_info"] = $item["classify_id"] === 0 ? (object) [] : \think\Db::name("app_feedback_classify")->where(["appid" => $item["appid"], "id" => $item["classify_id"]])->field("id,name,cover")->find();
		$file_list = [];
		foreach (json_decode($item["file_ids"]) as $file_id) {
			$file = \think\Db::name("public_upload_log")->where(["id" => $file_id])->find();
			if (!empty($file)) {
				if (empty($file["channel"])) {
					if ($file["type"] == 0) {
						$file["url"] = get_static_resources_url($file["uri"]);
					} elseif ($file["type"] == 1) {
						$file["url"] = get_static_resources_url($file["uri"], 1);
					} elseif ($file["type"] == 2) {
						$file["url"] = get_static_resources_url($file["uri"], 2);
					}
				}
				$file_list[] = $file;
			} else {
				$file_list[] = [];
			}
		}
		$data["file_list"] = $file_list;
		return $data;
	}
	public static function get_data_info($options)
	{
		$item = \think\Db::name("app_feedback_list")->where(["id" => $options["id"]])->find();
		if (empty($item)) {
			return ["code" => \app\system\model\Code::fail, "msg" => "id不存在"];
		}
		return ["code" => \app\system\model\Code::success, "data" => self::get_array_info($item)];
	}
	public static function edit_data($options)
	{
		\think\Db::startTrans();
		try {
			$row = \think\Db::name("app_feedback_list")->where(["id" => $options["id"]])->find();
			if (empty($row)) {
				return ["code" => \app\system\model\Code::fail, "msg" => "id不存在"];
			}
			$data = ["title" => $options["title"] ?? $row["title"], "classify_id" => $options["classify_id"] ?? $row["classify_id"], "content" => $options["content"], "device_info" => $options["device_info"], "device_code" => $options["device_code"], "read_status" => $options["read_status"] ?? $row["read_status"]];
			if (isset($options["classify_id"]) && $options["classify_id"] && \think\Db::name("app_feedback_classify")->where(["appid" => $options["appid"], "id" => $options["classify_id"]])->count() == 0) {
				return ["code" => \app\system\model\Code::fail, "msg" => "分类不存在"];
			}
			if (isset($options["handle_status"]) && $options["handle_status"] != $row["handle_status"]) {
				$data["handle_status"] = $options["handle_status"];
				if ($options["handle_status"] == 0) {
					$data["handle_time"] = 0;
					$data["complete_time"] = 0;
				} elseif ($options["handle_status"] == 1) {
					$data["handle_time"] = time();
					$data["complete_time"] = 0;
				} elseif ($options["handle_status"] == 2) {
					$data["handle_time"] = $row["handle_time"] ?: time();
					$data["complete_time"] = time();
				} elseif ($options["handle_status"] == 3) {
					$data["handle_time"] = $row["handle_time"] ?: time();
					$data["complete_time"] = time();
				}
			}
			\think\Db::name("app_feedback_list")->where(["id" => $options["id"]])->update($data);
			\think\Db::commit();
			return ["code" => \app\system\model\Code::success];
		} catch (\Exception $e) {
			\think\Db::rollback();
			return ["code" => \app\system\model\Code::error, "msg" => $e->getMessage()];
		}
	}
	public static function del_data($options)
	{
		\think\Db::startTrans();
		try {
			$new = \think\Db::name("app_feedback_list")->where(["id" => $options["id"]])->find();
			if (empty($new)) {
				return ["code" => \app\system\model\Code::fail, "msg" => "id不存在"];
			}
			\think\Db::name("app_feedback_list")->where(["id" => $options["id"]])->delete();
			\think\Db::commit();
			return ["code" => \app\system\model\Code::success];
		} catch (\Exception $e) {
			\think\Db::rollback();
			return ["code" => \app\system\model\Code::error, "msg" => $e->getMessage()];
		}
	}
	public static function get_classify_list($options)
	{
		$where[] = ["appid", "=", $options["appid"]];
		if (isset($options["id"]) && $options["id"] != "") {
			$where[] = ["id", "=", $options["id"]];
		}
		if (isset($options["name"]) && $options["name"] != "") {
			$where[] = ["name", "like", "%{$options["name"]}%"];
		}
		$list = \think\Db::name("app_feedback_classify")->where($where)->order(["sort" => "desc", "id" => "asc"])->page($options["page"], $options["limit"])->select();
		$count = \think\Db::name("app_feedback_classify")->where($where)->count();
		$pages = ceil($count / $options["limit"]);
		$data = [];
		foreach ($list as $item) {
			$data[] = $item;
		}
		return ["code" => \app\system\model\Code::success, "data" => $data, "count" => $count, "pages" => $pages];
	}
	public static function get_classify_info($options)
	{
		$item = \think\Db::name("app_feedback_classify")->where(["id" => $options["id"]])->find();
		if (empty($item)) {
			return ["code" => \app\system\model\Code::fail, "msg" => "id不存在"];
		}
		return ["code" => \app\system\model\Code::success, "data" => $item];
	}
	public static function add_classify($options)
	{
		\think\Db::startTrans();
		try {
			\think\Db::name("app_feedback_classify")->insert(["appid" => $options["appid"], "name" => $options["name"], "cover" => $options["cover"] ?? "", "sort" => $options["sort"], "addtime" => time()]);
			\think\Db::commit();
			return ["code" => \app\system\model\Code::success];
		} catch (\Exception $e) {
			\think\Db::rollback();
			return ["code" => \app\system\model\Code::error, "msg" => $e->getMessage()];
		}
	}
	public static function edit_classify($options)
	{
		\think\Db::startTrans();
		try {
			$row = \think\Db::name("app_feedback_classify")->where(["id" => $options["id"]])->find();
			if (empty($row)) {
				return ["code" => \app\system\model\Code::fail, "msg" => "id不存在"];
			}
			$data = ["name" => $options["name"], "cover" => $options["cover"], "sort" => $options["sort"]];
			\think\Db::name("app_feedback_classify")->where(["id" => $options["id"]])->update($data);
			\think\Db::commit();
			return ["code" => \app\system\model\Code::success];
		} catch (\Exception $e) {
			\think\Db::rollback();
			return ["code" => \app\system\model\Code::error, "msg" => $e->getMessage()];
		}
	}
	public static function del_classify($options)
	{
		\think\Db::startTrans();
		try {
			$new = \think\Db::name("app_feedback_classify")->where(["id" => $options["id"]])->find();
			if (empty($new)) {
				return ["code" => \app\system\model\Code::fail, "msg" => "id不存在"];
			}
			\think\Db::name("app_feedback_classify")->where(["id" => $options["id"]])->delete();
			\think\Db::commit();
			return ["code" => \app\system\model\Code::success];
		} catch (\Exception $e) {
			\think\Db::rollback();
			return ["code" => \app\system\model\Code::error, "msg" => $e->getMessage()];
		}
	}
}